package org.example.bookstore.controller;


import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.bookstore.custom.ResultVO;
import org.example.bookstore.pojo.DTO.QiniuDTO;
import org.example.bookstore.service.UploadImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@Tag(name = "头像上传Api")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/qiniu")
public class UploadController {

    @Autowired
    private UploadImageService uploadImageService;

    //七牛云新开空间设置  1 修改配置文件 2 设置对象存储空间为--公开

    // 有个bug 但重新启动项目就好了
    @PostMapping("/uploadImage")
    public ResultVO<QiniuDTO> upLoadImage(@RequestParam("file") MultipartFile file) {
        log.info("/qiniu/uploadImage");
        System.out.println(file);
        QiniuDTO qiniuDTO = new QiniuDTO();
        if (!file.isEmpty()) {
            String path = uploadImageService.uploadQNImg(file);
            if ("失败".equals(path)) {
                return ResultVO.success("上传失败");
            } else {
                System.out.println("七牛云返回的图片链接是：" + path);
                qiniuDTO.setPath(path);
                return ResultVO.success(qiniuDTO);
            }
        }
        return ResultVO.success("上传失败");
    }
}